<?php
/*
 * Created on 2013-10-13
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

class UploadController extends Controller{
	public function fileup() {
		global $fileload;
		if ($_FILES ['msUploadFile'] ['size'] < 1024 * 1024 * 2) {
			$dirroot = PROJECT.DS."upload".DS."dataimport".DS;//上传路径
			$filename = explode ( ".", $_FILES ['msUploadFile'] ['name'] );
			$filename [0] = strtotime ( date ( 'Y-m-d' ) ) . "-" . rand (); // 设置随机数长度
			$file_name = implode ( ".", $filename );
			$uploadfile = $dirroot . $file_name;
			move_uploaded_file ( $_FILES ['msUploadFile'] ['tmp_name'], $uploadfile );
			
			self::importData($uploadfile);
			$fileload->type = "1";
			$fileload->message = "上传成功！ ";
			$fileload->filename =  $file_name;
		} else {
			$fileload->type = "0";
			$fileload->message = "请上传小于2MB的jpeg或Gif类型的附件);";
		}
		echo json_encode ( $fileload );
	}
	 
	function importData($filename){
      $this->getSmarty ();
      require_once EXCEL.DS.'reader.php';
      require_once SERVICE . DS . 'ResourceService.class.php';

      $resourceService = new ResourceService ( $this->getDB () );
      $data = new Spreadsheet_Excel_Reader();
      $data->setOutputEncoding('utf-8');
      $data->read($filename);
/**
      for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
	     for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
		    if($j==$data->sheets[0]['numCols']){
			      $str.="'".$data->sheets[0]['cells'][$i][$j]."'";
		       }else{
			      $str.="'".$data->sheets[0]['cells'][$i][$j]."',";
		       }
	      }
	      $str="(".$str.")";
	      $resource_infos = $resourceService->uploads ($str);
      }
*/
      /**-----关于BUG与测试------/
       *  //上面的代码首先是存在BUG的。先说BUG,$str，在一直不停的增加.所以，你在打印
          // 异常信息中就能看到sql语句是不对的。一定要测试每一句的正确性，才能保存最
         //终的正确。当然，每一句可能有点难，那么，你至少先测试输出所有的sql是正确的
         //然后在确定入库正常与否
       */

       $pre_sql="insert into TCA_RESOURCE_INFO (DEVICE_NAME,DEVICE_TYPE,DEVICE_CATEGORY,".
            "WORK_TYPE,BELONG_SYSTEM,LOCATION,SATELLITE,VENDOR,PURCHASE_TIME,PRICE,GURANTEE_TIME,START_TIME,GURANTEE_VENDOR,CONTACT_NAME,CONTACT_TYPE) values ";

       //这一句一写，就可以测试是否正确了 echo $rowNums
      $rowNums = $data->sheets[0]['numRows'];
      $colNums = $data->sheets[0]['numCols'];
      $startRow = 2;
      
      //print ($colNums."".$rowNums);
	for ($row = $startRow;$row <= $rowNums; $row++) {
		//每次将sql串置空
		 $sqlStr = "";
		 $SATELLITE = "";
		 $WORK_TYPE = "";
		 $BELONG_SYSTEM = "";
		 $LOCATION = "";
		 
	     for ($col = 1; $col <= $colNums; $col++) {
	     	  $celldata=$data->sheets[0]['cells'][$row][$col];
	     	  if($celldata==null){
                    $celldata="NULL";
	     	  }
		      if($col==$colNums){
                  if($celldata=="NULL"){$sqlStr.= "'"."'";}else{$sqlStr.= "'".$celldata."'";}

		       }else{
		       	 if($celldata=="NULL"){$sqlStr.= "'"."',";}else{$sqlStr.= "'".$celldata."',";}
		       }
		       if($col == 7){
		       	$SATELLITE = $celldata;
		       }
		       if($col == 4){
		       	$WORK_TYPE = $celldata;
		       }
		       if($col == 5){
		       	$BELONG_SYSTEM = $celldata;
		       }
		       if($col == 6){
		       	$LOCATION = $celldata;
		       }
	      }

	      $sqlStr="(".$sqlStr.")";
	      $sql = $pre_sql.$sqlStr;
// 	      echo $sql;
	     // echo $pre_sql.$sqlStr."</br>"; //这样把sql拼起来，打印出sql语句看一下，就会发现sql语句的问题，数据是否准备等。
	                                 //在这一步，把问题都检查好了以后，确保sql语句语法正确，数据正确后，在说入库
	                                 //写好程序的秘诀就是拆好程序.在这一步，发现了很多问题，txt给你了，脚本暂时改了。
	       //确认sql没有问题了，执行入库，入库的service可以简单一点，就是执行脚本
	       //看一下，执行一个sql语句调的是query方法，insert方法不是调用sql的方式，是数组的方式
	      $resource_infos = $resourceService->uploads($sql);
	      //print ("resource_infos::::".$resource_infos."sql:::".$sql);
	      $resourceService->insertDicResource($SATELLITE,$WORK_TYPE,$BELONG_SYSTEM,$LOCATION);
      }

	}
     

}

?>
